package com.onekbase.demo.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyMain {
    public static void main(String[] args) {
        Object p = Proxy.newProxyInstance(ProxyMain.class.getClassLoader(),
                new Class[] { IEcho.class,IHello.class }, new ProxyDemo());
        ((IHello)p).hello();
        ((IEcho)p).echo("test");
    }

    interface IEcho{
        void echo(String str);
    }

    interface IHello{
        void hello();
    }

     static class ProxyDemo implements InvocationHandler {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if(method.getName().equals("hello")){
                System.out.println("proxy hello");
            }
            if(method.getName().equals("echo")){
                System.out.println("proxy echo "+args[0]);
            }
            return true;
        }
    }
}
